import java.util.Stack;

public class Solution921 {
    public int minAddToMakeValid(String s) {
        Stack<Character> stack=new Stack<>();
        int ans=0;
        for (char c : s.toCharArray()) {
            if (c=='('){
                stack.push(c);
            }else{
                if (stack.isEmpty()){
                    ans++;
                }else {
                    stack.pop();
                }
            }
        }
        return ans+stack.size();
    }

    public static void main(String[] args) {
        System.out.println(new Solution921().minAddToMakeValid("())"));
    }
}
